﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using System.Text.RegularExpressions;

public partial class pass : System.Web.UI.Page
{
    protected string dParam
    {
        get
        {
            return (String.IsNullOrEmpty(Request["d"]) ? "" : Request["d"]);
        }
    }

    protected string qsParam
    {
        get
        {
            return (String.IsNullOrEmpty(Request["qs"]) ? "" : Request["qs"]);
        }
    }
    protected string refUrl = string.Empty;
    protected string email = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["d"] == null && Request["qs"] == null)
        {
            divEmail.Visible = false;
            divMsg.Visible = true;
        }       
        if (!Page.IsPostBack)
            refUrl = string.Format("d={0}&qs={1}", Server.UrlEncode(dParam), Server.UrlEncode(qsParam));
        if (Request.HttpMethod == "POST")
        {
            email = Request["emailcuaban"].Trim();
            if (ValidateEmail(email))
            {
                a_User user = a_UserService.Geta_UserByLoginEmail(email);
                if (user == null)
                {
                    errorMsg.InnerHtml = "<p>&nbsp;</p> <p>&nbsp;Xin lỗi \"" + email + "\" này không tồn tại.</p> <p>&nbsp;</p>"; return;
                }
                StreamReader reader = new StreamReader(Server.MapPath("EmailTemplates/ForgotPassword.htm"));
                string content = reader.ReadToEnd();
                content = content.Replace("[SenderName]", user.Firstname);
                content = content.Replace("[EmailAddress]", user.Login_Email);
                content = content.Replace("[Password]", user.Password);
                EmailHelper.SendMailForgotPassword(email, content);
                Response.Redirect(String.Format("pass.aspx?ckid={0}", SessionManager.GetCookie()), true);
            }
            else
            {
                errorMsg.InnerHtml = "<p>&nbsp;</p> <p>&nbsp;Xin vui lòng nhập lại email.</p> <p>&nbsp;</p>";
            }
        }
    }

    protected override void OnInit(EventArgs e)
    {
        //btnSubmit.Click += new EventHandler(btnSubmit_Click);
        base.OnInit(e);
    }

    //protected void btnSubmit_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        if (ValidateEmail())
    //        {
    //            a_User user = a_UserService.Geta_UserByLoginEmail(txtEmail.Value.Trim());
    //            StreamReader reader = new StreamReader(Server.MapPath("ForgotPassword.htm"));
    //            string content = reader.ReadToEnd();
    //            content = content.Replace("[SenderName]", user.Firstname);
    //            content = content.Replace("[EmailAddress]", user.Login_Email);
    //            content = content.Replace("[Password]", user.Password);
    //            EmailHelper.SendMailForgotPassword(txtEmail.Value.Trim(), content);
    //            Response.Redirect(String.Format("pass.aspx?ckid={0}", SessionManager.GetCookie()));
    //        }
    //        else
    //        {
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //}

    protected bool ValidateEmail(string email)
    {
        Regex r = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        if (email == "" || !r.IsMatch(email))
            return false;
        return true;
    }
}
